package com.tl.commonlibrary.ui.web;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.webkit.WebView;

import com.tl.commonlibrary.R;
import com.tl.commonlibrary.ui.BaseFragmentActivity;


public class WebActivity extends BaseFragmentActivity {
    private static final String WEB_TAG = "tag_web_activity_comm";
    private static final String KEY_WEB_BAR_TITLE = "title";
    private static final String KEY_WEB_FROM_TYPE = "fromType";
    public static final String LOAD_HTML = "loadNativeHtml";
    private WebModel model;
    private WebViewModel viewModel;

    private static final Intent newIntent(Context context, String url, String title, int fromType) {
        return newIntent(context, url, title, false, null, fromType);
    }
    public static final Intent newIntent(Context context, String title, int fromType) {
        return newIntent(context, LOAD_HTML, title, false, null, fromType);
    }
    /**
     * @param context
     * @param url            地址
     * @param title          标题
     * @param canGoBack      按下物理返回键是否可以返回上一级
     * @param javascriptName JavaScript别名
     * @param fromType       来自哪里
     * @return
     */
    public static final Intent newIntent(Context context, String url, String title, boolean canGoBack, String javascriptName, int fromType) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(WebFragment.KEY_WEB_URL, url);
        intent.putExtra(KEY_WEB_BAR_TITLE, title);
        intent.putExtra(WebFragment.KEY_WEB_CAN_GO_BACK, canGoBack);
        intent.putExtra(WebFragment.KEY_WEB_JS_NAME, javascriptName);
        intent.putExtra(KEY_WEB_FROM_TYPE, fromType);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        SoftHideKeyBoardUtil.assistActivity(this);
        initData();

    }

    private final void initData() {
        model = new WebModel(this);
        viewModel = new WebViewModel(this);
        if (getIntent() != null) {
            model.setActionBarTitle(getIntent().getStringExtra(KEY_WEB_BAR_TITLE));
            model.setFromType(getIntent().getIntExtra(KEY_WEB_FROM_TYPE, WebModel.FROM_TYPE_DEFAULT));
        }
        viewModel.setActionBar();
        loadWebFragment();
    }

    public WebModel getModel() {
        return model;
    }

    public WebViewModel getViewModel() {
        return viewModel;
    }

    public void loadUrl() {
        if (getViewModel() != null) {
            WebFragment webFragment = getViewModel().getWebFragment();
            if (webFragment != null) {
               webFragment.loadUrl();
            }
        }
    }
    public void loadHtml(String data){
        if (getViewModel() != null) {
            WebFragment webFragment = getViewModel().getWebFragment();
            if (webFragment != null) {
                webFragment.loadHtml(data);
            }
        }
    }
    public void loadCurrentUrl(){
        WebView webView = getWebView();
        if(webView != null) {
            webView.reload();
        }
    }
    private String url = null;

    /**
     * 加载WebFragment
     */
    private void loadWebFragment() {
        boolean canGoBack = false;
        String javaScriptName = null;
        if (getIntent() != null) {
            url = getIntent().getStringExtra(WebFragment.KEY_WEB_URL);
            canGoBack = getIntent().getBooleanExtra(WebFragment.KEY_WEB_CAN_GO_BACK, true);
            javaScriptName = getIntent().getStringExtra(WebFragment.KEY_WEB_JS_NAME);
        }
        if (!WebFragmentModel.isUrl(url)&& !url.equals(LOAD_HTML)) {
            finish();
            return;
        }
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        WebFragment webFragment = WebFragment.newInstance(url, canGoBack, javaScriptName);
        transaction.replace(R.id.web_container, webFragment, WEB_TAG);
        transaction.commitAllowingStateLoss();
        viewModel.setWebFragment(webFragment);
    }

    //    /**
//     * 获取WebFragment中的url
//     *
//     * @return
//     */
//    public String getUrl() {
//        if (viewModel != null && viewModel.getWebFragment() != null) {
//            return viewModel.getWebFragment().getUrl();
//        }
//        return null;
//    }
    public String getOriginUrl() {
        return url;
    }

    /**
     * 从开启的来源解析Intent数据
     */
    private String parseIntentByOpenSource() {
        //拿到Url信息
        Intent openIntent = getIntent();
        String action = openIntent.getAction();
        //判断来源action
        if (Intent.ACTION_VIEW.equals(action)) {
            //拿到uri信息
            Uri uri = openIntent.getData();
            if (uri == null) {
                //异常状态,关闭当前页面
                finish();
                return "";
            }
            //拿到其中的URL信息
            return uri.getQueryParameter("information");
        }
        return "";
    }

    public WebView getWebView() {
        if (viewModel != null) {
            WebFragment webFragment = viewModel.getWebFragment();
            if (webFragment != null) {
                return webFragment.getWebView();
            }
        }
        return null;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
//        if (ActivityStack.get().has(WebullMainActivity.class) <= 0) {
//            ActivityStack.get().popAll();
//            startActivity(new Intent(this, WebullMainActivity.class));
//        }
    }


    public static void start(Context context, String url) {
        context.startActivity(newIntent(context, url, "", WebModel.FROM_TYPE_DEFAULT));
    }
    public static void startForHtml(Context context) {
        context.startActivity(newIntent(context, "", WebModel.FROM_TYPE_DEFAULT));
    }


    public static void start(Context context, String url, int fromType) {
        context.startActivity(newIntent(context, url, "", fromType));
    }

    public static void start(Context context, String url, String title) {
        context.startActivity(newIntent(context, url, title, WebModel.FROM_TYPE_DEFAULT));
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }
}
